import Common from '../../common/common.js';
import FriendListItem from './FriendListItem.js';
import ServiceNet from '../../servicenet.js';
import BaseList from '../base/BaseList.js';


const logger = Common.log("FriendList");

function FriendList () {
    BaseList.call(this);
}
let prototype = Object.create(BaseList.prototype);
prototype.constructor = FriendList;
FriendList.prototype  = prototype;

//初始化好友数据
FriendList.prototype.initData = function(){
    //获取用户列表，一次性拉取所有用户
    let userList = [];
    const netFriendList = (startIndex) => {
        logger.debug("拉取好友列表：startIndex", startIndex);
        ServiceNet.getFriendList(startIndex, (d) => {
            let nextStartIndex = d.nextStartIndex;
            //整理userID
            if (d.friendData.length > 0) {
                d.friendData.forEach((t, i) => {
                    userList.push({userID:t.userId,remark:t.remark});
                })

                if(nextStartIndex != 0){
                    netFriendList(nextStartIndex);
                }else{
                    //全部获取完毕
                    this.setUserList(userList);
                }
            }
        });
    }
    netFriendList(0);
}

FriendList.prototype.setUserList = function(userList){
    this.setItems( userList.map(t=>new FriendListItem().updateDataByUserID(t.userID,t.remark)));
}



export default FriendList;
